package com.ctvit.cch.web.controller;

import com.ctvit.cch.core.pojo.dto.creative.CreativeShotDesignGenerateSidDTO;
import com.ctvit.cch.core.pojo.dto.creative.CreativeShotDesignRegenerateSidDTO;
import com.ctvit.cch.core.pojo.vo.creative.CreativeShotFrameGetVO;
import com.ctvit.cch.core.pojo.vo.creative.CreativeShotFrameListVO;
import com.ctvit.cch.core.service.ICreativeShotDesignService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import jakarta.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 分镜设计Controller
 *
 * @author heyingcheng
 * @date 2025/10/11
 */
@RestController
@RequestMapping("/creative/shot/design")
public class CreativeShotDesignController extends BaseController {

    private final ICreativeShotDesignService creativeShotDesignService;

    public CreativeShotDesignController(ICreativeShotDesignService creativeShotDesignService) {
        this.creativeShotDesignService = creativeShotDesignService;
    }

    /**
     * 生成分镜设计
     * 
     * @param dto 生成分镜设计参数
     * @return {@link AjaxResult} 生成结果
     */
    @Log(title = "分镜设计-生成分镜", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('creative:shot:design:generate')")
    @PostMapping("/generate")
    public AjaxResult generate(@RequestBody @Validated CreativeShotDesignGenerateSidDTO dto) {
        return toAjax(creativeShotDesignService.generate(dto));
    }

    /**
     * 重新生成分镜设计
     * 
     * @param dto 重新生成分镜设计参数
     * @return {@link AjaxResult} 生成结果
     */
    @Log(title = "分镜设计-重新生成分镜", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('creative:shot:design:generate')")
    @PostMapping("/regenerate")
    public AjaxResult regenerate(@RequestBody @Validated CreativeShotDesignRegenerateSidDTO dto) {
        return toAjax(creativeShotDesignService.regenerate(dto));
    }

    /**
     * 按脚本ID获取分镜设计详细信息
     *
     * @param scriptId 脚本ID
     * @return {@link AjaxResult} 详细信息
     */
    @PreAuthorize("@ss.hasPermi('creative:shot:design:get_by_script')")
    @PostMapping(value = "/get/by/script")
    public AjaxResult getByScriptId(@RequestParam String scriptId) {
        return success(creativeShotDesignService.getByScriptId(scriptId));
    }

    /**
     * 按选题ID获取分镜设计详细信息
     *
     * @param topicId 选题ID
     * @return {@link AjaxResult} 详细信息
     */
    @PreAuthorize("@ss.hasPermi('creative:shot:design:get_by_topic')")
    @PostMapping(value = "/get/by/topic")
    public AjaxResult getByTopicId(@RequestParam String topicId) {
        return success(creativeShotDesignService.getByTopicId(topicId));
    }

    @Log(title = "分镜设计-下载分镜", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('creative:script:design:export')")
    @PostMapping("/export/by/script")
    public void export(HttpServletResponse response, @RequestParam String scriptId)
            throws UnsupportedEncodingException {
        CreativeShotFrameGetVO vo = creativeShotDesignService.getByScriptId(scriptId);
        List<CreativeShotFrameListVO> frames = vo.getFrames();
        ExcelUtil<CreativeShotFrameListVO> util = new ExcelUtil<CreativeShotFrameListVO>(
                CreativeShotFrameListVO.class);
        util.exportExcel(response, frames, "分镜设计");
    }

}